package com.xintonghua.bussiness.ui.user.manage;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xintonghua.bussiness.BuildConfig;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.TraceAdapter;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.CommCodeBean;
import com.xintonghua.bussiness.bean.LogisticsResponse;
import com.xintonghua.bussiness.bean.StockOrderBean;
import com.xintonghua.bussiness.bean.Traces;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：wanxianyi
 * 时间：2018/4/24.
 * 包名：com.xintonghua.bussiness.ui.user.manage
 */

public class SelectSaltActivity extends BaseActivity {
    @BindView(R.id.main_pic_iv)
    ImageView mainPicIv;
    @BindView(R.id.express_status_tv)
    TextView expressStatusTv;
    @BindView(R.id.express_company_tv)
    TextView expressCompanyTv;
    @BindView(R.id.express_number_tv)
    TextView expressNumberTv;
    private String orderNumber;
    @BindView(R.id.traceRv)
    RecyclerView traceRv;
    private TraceAdapter mAdapter;
    StockOrderBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistics);
        ButterKnife.bind(this);
        onSimpleActionBar();
        setSimpleActionBar("查看物流");
        bean = getIntent().getParcelableExtra("data");
        orderNumber = getIntent().getStringExtra(BuildConfig.FLAVOR);
        expressStatusTv.setText("订单号：" + bean.getNumber());
        expressCompanyTv.setText("运单编号：" + orderNumber);
        expressNumberTv.setText("更新时间：" + bean.getEnd_time());
        getComCode();
    }

    private void getComCode() {
        RequestParams params = new RequestParams("http://www.kuaidi100.com/autonumber/auto");
        params.addParameter("num", orderNumber);
        params.addParameter("key", "pKpwrMEP5950");
        x.http().get(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                List<CommCodeBean> commCodeBeans = JSONObject.parseArray(result, CommCodeBean.class);
                CommCodeBean commCodeBean = commCodeBeans.get(0);
                logistics(commCodeBean.getComCode(), orderNumber);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    private void logistics(String comCode, String nu) {
        RequestParams params = new RequestParams("http://api.kuaidi100.com/api");
        params.addParameter("id", "29833628d495d7a5");
        params.addParameter("com", comCode);
        params.addParameter("nu", nu);
        params.addParameter("valicode", "");
        params.addParameter("show", "0");
        params.addParameter("muti", 1);
        params.addParameter("order", "desc");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogisticsResponse response = JSONObject.parseObject(result, LogisticsResponse.class);
                List<Traces> mTraceList = response.getData();
                if (mTraceList != null && mTraceList.size() > 0) {
                    expressNumberTv.setText("更新时间："+mTraceList.get(0).getTime());
                }
                LinearLayoutManager layoutManager = new LinearLayoutManager(SelectSaltActivity.this, OrientationHelper.VERTICAL, false);
                mAdapter = new TraceAdapter(SelectSaltActivity.this, mTraceList);
                traceRv.setLayoutManager(layoutManager);
                traceRv.setAdapter(mAdapter);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
